import pandas as pd
import numpy as np


def main():

    file_names_e1 = ['../results/ATI/optimized_thresholds.csv',
    '../results/ATI/LP.csv',
    '../results/ATI/rakel_2.csv',
    '../results/ATI/CC.csv',
    '../results/ATI/SMOTE.csv',
    '../results/ATI/BR.csv',
    '../results/ATI/rakel_4.csv',
    '../results/ATI/optimized_models.csv',
    '../results/ATI/ECC.csv',
    '../results/ATI/MLKNN.csv']

    file_names_e4 = [
    '../results/ATI/BR.csv',
    '../results/ATI/CNN.csv',
    '../results/ATI/ECC.csv',
    '../results/ATI/LSTM.csv']

    temp_list = []
    for file in file_names_e1:
        temp_df = pd.read_csv(file).drop(['Unnamed: 0'],axis=1)
        model_name = file.split('/')[-1][:-4]
        temp_df['model'] = model_name
        temp_list.append(temp_df)
    pd.concat(temp_list).groupby('model').mean()[['acc','hamming','r_loss','micro','macro']].to_csv('../results/ATI/table_e1_mean.csv')
    pd.concat(temp_list).groupby('model').std()[['acc','hamming','r_loss','micro','macro']].to_csv('../results/ATI/table_e1_std.csv')

    temp_list = []
    for file in file_names_e4:
        temp_df = pd.read_csv(file).drop(['Unnamed: 0'],axis=1)
        model_name = file.split('/')[-1][:-4]
        temp_df['model'] = model_name
        temp_list.append(temp_df)
    pd.concat(temp_list).groupby('model').mean()[['acc','hamming','r_loss','micro','macro']].to_csv('../results/ATI/table_e4_mean.csv')
    pd.concat(temp_list).groupby('model').std()[['acc','hamming','r_loss','micro','macro']].to_csv('../results/ATI/table_e4_std.csv')


    df = pd.read_csv('../results/ATI/table_e1_mean.csv')
    col_names = ['acc', 'hamming', 'r_loss', 'micro', 'macro']

    all_score = []
    for col in col_names:
        scores = np.zeros((10,1))
        if col in ['acc', 'micro', 'macro']:
            selected = np.argsort(np.round(df[col].values,2))[-3:]
            scores[selected,0] = 1
        else:
            selected = np.argsort(np.round(df[col].values,2))[:3]
            scores[selected,0] = 1
        all_score.append(scores)

    out_df = df.copy()
    out_df.loc[:,'acc':] = np.array(all_score).reshape(5,10).T
    out_df.to_csv('../results/ATI/table_2.csv')

if __name__ == "__main__":
    main()